package com.ybxx.base.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.Community;
import com.ybxx.base.model.Fees;
import com.ybxx.base.model.FeesItem;
import com.ybxx.base.model.FeesType;
import com.ybxx.base.service.CommunityService;
import com.ybxx.base.service.FeesItemService;
import com.ybxx.base.service.FeesService;
import com.ybxx.base.service.FeesTypeService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;

public class FeesAction extends BaseAction {
	@Resource
	private FeesService feesService;
	@Resource
	private CommunityService communityService;
	@Resource
	private FeesItemService feesItemService;
	@Resource
	private FeesTypeService feesTypeService;
	private List<Fees> feesex;
	private List<FeesItem> feesItems;
	private List<Community> communitys;
	private List<FeesType> feesTypes;
	private String cid;
	private boolean editsign=true;
	private Community community;
	private Fees fees;
	private String ftids;
	private String prices;
	private String msg="succ";
	/*查看*/
	public String view(){
		community=communityService.queryFees(cid);
		feesItems=feesItemService.queryFeesItem(cid);
		fees=feesService.queryFees(cid);
		return "view";
	}
	/*删除*/
	public String remove(){
		try{
			fees=feesService.queryFees(cid);
			feesService.remove(fees);
		}catch(Exception ex){
			msg="卫生费删除失败.";
		}
		return "succ";
	}
	/*保存*/
	public String save(){
		try{
			if(fees!=null && StringUtils.isEmpty(fees.getFid())){
				fees.setFid(UUIDGenerator.getUUID());
			}
			if(ftids!=null && !"".equals(ftids)){
				String[] ftid=ftids.split(",");
				String[] price=prices.split(",");
				for(int i=0;i<ftid.length;i++){
					FeesItem fi=new FeesItem();
					fi.setFees(fees);
					fi.setFeesType(new FeesType(ftid[i]));
					fi.setFicode(UUIDGenerator.getUUID());
					fi.setPrice(price[i]);
					fees.getFeesItems().add(fi);
				}
			}
			feesService.save(fees);
		}catch(Exception ex){
			ex.printStackTrace();
			msg="卫生费保存失败.";
		}
		return "succ";
	}
	/*加载修改，新增的数据*/
	public String load(){
		feesTypes=feesTypeService.getAll();
		if(cid==null || "".equals(cid)){//新增
			User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
			communitys=communityService.queryNotFees(user.getLoginid());
		}else{//修改
			editsign=false;
			community=communityService.queryFees(cid);
			feesItems=feesItemService.queryFeesItem(cid);
			fees=feesService.queryFees(cid);
		}
		return "load";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		/**获取当前登录者*/
		User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
		communitys=communityService.queryFeesPage(filter,user.getLoginid());
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	public List<Fees> getFeesex() {
		return feesex;
	}
	public void setFeesex(List<Fees> feesex) {
		this.feesex = feesex;
	}
	public List<Community> getCommunitys() {
		return communitys;
	}
	public void setCommunitys(List<Community> communitys) {
		this.communitys = communitys;
	}
	public List<FeesItem> getFeesItems() {
		return feesItems;
	}
	public void setFeesItems(List<FeesItem> feesItems) {
		this.feesItems = feesItems;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public boolean isEditsign() {
		return editsign;
	}
	public void setEditsign(boolean editsign) {
		this.editsign = editsign;
	}
	public Community getCommunity() {
		return community;
	}
	public void setCommunity(Community community) {
		this.community = community;
	}
	public Fees getFees() {
		return fees;
	}
	public void setFees(Fees fees) {
		this.fees = fees;
	}
	public String getFtids() {
		return ftids;
	}
	public void setFtids(String ftids) {
		this.ftids = ftids;
	}
	public String getPrices() {
		return prices;
	}
	public void setPrices(String prices) {
		this.prices = prices;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<FeesType> getFeesTypes() {
		return feesTypes;
	}
	public void setFeesTypes(List<FeesType> feesTypes) {
		this.feesTypes = feesTypes;
	}
	
}
